package com.csun.dsms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.TicketProblemInfoMapper;
import com.csun.dsms.mapper.TicketProblemPercentMapper;
import com.csun.dsms.model.domain.TicketProblemPercent;
import com.csun.dsms.model.process.TicketProblemInfo;
import com.csun.dsms.model.qo.TicketProblemPercentAddQo;
import com.csun.dsms.model.qo.TicketProblemPercentUpdQo;
import com.csun.dsms.model.vo.ticketproblem.TicketProblemPercentVo;
import com.csun.dsms.service.TicketProblemPercentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description
 *
 * @author Hongd
 * @since 2025-08-04
 */
@Slf4j
@Service
public class TicketProblemPercentServiceImpl implements TicketProblemPercentService {

    @Resource
    private TicketProblemPercentMapper ticketProblemPercentMapper;
    @Resource
    private TicketProblemInfoMapper ticketProblemInfoMapper;

    @Override
    public Wrapper<Void> addTicketProblemPercent(List<TicketProblemPercentAddQo> addQoList) {
        if (CollectionUtil.isEmpty(addQoList)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "参数不能为空");
        }
         addQoList.forEach(addQo -> {
            TicketProblemPercent ticketProblemPercent = new TicketProblemPercent();
            ticketProblemPercent.setTicketProblemInfoId(addQo.getTicketProblemInfoId());
            ticketProblemPercent.setAcceptStandard(addQo.getAcceptStandard());
            ticketProblemPercent.setTarget(addQo.getTarget());
            ticketProblemPercent.setWeight(addQo.getWeight());
            ticketProblemPercent.setCurrent(addQo.getCurrent());
            ticketProblemPercent.setCreatedTime(new Date());
            ticketProblemPercent.setCreatedBy(addQo.getCreatedBy());
             ticketProblemPercentMapper.insertSelective(ticketProblemPercent);
        });
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功");
    }

    @Override
    public Wrapper<List<TicketProblemPercentVo>> listTicketProblemPercent(Integer ticketProblemInfoId) {
        TicketProblemPercent ticketProblemPercentQuery = new TicketProblemPercent();
        ticketProblemPercentQuery.setTicketProblemInfoId(ticketProblemInfoId);
        List<TicketProblemPercent> ticketProblemPercentList = ticketProblemPercentMapper.select(ticketProblemPercentQuery);
        List<TicketProblemPercentVo> ticketProblemPercentVoList = ticketProblemPercentList.stream().map(ticketProblemPercent -> {
            TicketProblemPercentVo ticketProblemPercentVo = new TicketProblemPercentVo();
            BeanUtils.copyProperties(ticketProblemPercent, ticketProblemPercentVo);
            return ticketProblemPercentVo;
        }).collect(Collectors.toList());
        return WrapMapper.ok(ticketProblemPercentVoList);
    }

    @Override
    public Wrapper<Void> updateTicketProblemPercent(List<TicketProblemPercentUpdQo> updQoList) {
        if (CollectionUtil.isEmpty(updQoList)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "参数不能为空");
        }
        updQoList.forEach(updQo -> {
            TicketProblemPercent ticketProblemPercent = new TicketProblemPercent();
            BeanUtils.copyProperties(updQo, ticketProblemPercent);
            ticketProblemPercentMapper.updateByPrimaryKey(ticketProblemPercent);
        });

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功");
    }

    @Override
    public Wrapper<Double> scopeTicketProblemPercent(Integer ticketProblemInfoId) {
        TicketProblemPercent ticketProblemPercentQuery = new TicketProblemPercent();
        ticketProblemPercentQuery.setTicketProblemInfoId(ticketProblemInfoId);
        List<TicketProblemPercent> ticketProblemPercentList = ticketProblemPercentMapper.select(ticketProblemPercentQuery);
        Double score = 0.0;
        if (CollectionUtil.isEmpty(ticketProblemPercentList)){
            return WrapMapper.wrap(0);
        }
        for (TicketProblemPercent ticketProblemPercent : ticketProblemPercentList) {
            score =score + ticketProblemPercent.getCurrent()/ticketProblemPercent.getTarget() * ticketProblemPercent.getWeight()* 100;
        }
        TicketProblemInfo ticketProblemInfo = new TicketProblemInfo();
        ticketProblemInfo.setId(ticketProblemInfoId);
        ticketProblemInfo.setScore(score);
        ticketProblemInfoMapper.updateByPrimaryKey(ticketProblemInfo);
        return WrapMapper.ok(score);
    }
}
